<template>
  <el-dialog :visible.sync="currentVisible"
             width="395px"
             destroy-on-close
             custom-class="b-fast-modal"
             v-on="$listeners"
             append-to-body>
    <el-form class="b-fast-add-form form-margin--mini"
             :model="form"
             :rules="rules"
             label-width="80px"
             label-position="top"
             ref="form">
      <el-form-item :label="$t('custom.stcdm')"
                    class="b-fast-add-form--text"></el-form-item>
      <el-form-item prop="name">
        <el-input v-model="form.name"
                  :placeholder="$t('custom.petqfn')"></el-input>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button size="mini"
                 type="primary"
                 :loading="loading"
                 @click="onSave">{{$t('save')}}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { createFilter } from '@/api/new'
export default {
  name: 'BFastModal',
  props: {
    visible: Boolean,
    parameter: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      form: {
        name: undefined
      },
      rules: {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur', pattern: '[^ \x22]+' }
        ]
      },
      loading: false
    }
  },
  computed: {
    currentVisible: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('update:visible', !!val)
      }
    }
  },
  methods: {
    async onSave () {
      try {
        const value = await this.$refs.form.validate()
        if (value) {
          this.loading = true
          const { name } = this.form
          const params = {
            ...this.parameter,
            name
          }

          const { data: response } = await createFilter(params)
          if (response.code !== 0) {
            throw response.msg
          }

          this.$message.success(this.$t('success'))
          this.$emit('success')
          this.currentVisible = false
        }
      } catch (error) {
        if (typeof error === 'string') {
          this.$message.error(error)
        }
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.b-fast-add-form {
  &--text {
    ::v-deep .el-form-item__label {
      font-size: 14px;
      color: #404040;
      line-height: 18px;
    }
  }
}
</style>
